package com.example;

import org.junit.jupiter.api.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTest {
    private static final String DATE_FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE = "yyyy-MM-dd HH:mm";
    private static final String DATE_FORMAT_YEAR_MONTH_DAY_HOUR = "yyyy-MM-dd HH";
    private static final String DATE_FORMAT_YEAR_MONTH_DAY = "yyyy-MM-dd";
    private static final String DATE_FORMAT_YEAR_MONTH = "yyyy-MM";
    private static final String DATE_FORMAT_YEAR = "yyyy";

    @Test
    public void testDate() {
        Date date = new Date();
        System.out.println(date);

        System.out.println("date.getTime() = " + date.getTime());   //
    }


    @Test
    public void testSimpleDateFormat() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String strDate = sdf.format(date);
        System.out.println(strDate);
    }

    @Test
    public void testLastToday() {
        Date date = new Date();

        // 过去一个小时，精确到小时
        long currentTime = date.getTime() - 1000L * 60 * 60;
        Date lastHour = new Date(currentTime);
        String lastHourStrDate = getSimpleDateFormatString(lastHour, DATE_FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
        System.out.println("过去一小时的时间：" + lastHourStrDate);

        // 过去一天
        Date lastDay = new Date(currentTime - 1000L * 60 * 60 * 24);
        String lastDayStrDate = getSimpleDateFormatString(lastDay, DATE_FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
        System.out.println("过去一天的时间：" + lastDayStrDate);

        // 过去一周
        Date lastWeek = new Date(currentTime - 1000L * 60 * 60 * 24 * 7);
        String lastWeekStrDate = getSimpleDateFormatString(lastWeek, DATE_FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
        System.out.println("过去一周的时间：" + lastWeekStrDate);

        // 过去一个月
        Date lastMonth = new Date(currentTime - 1000L * 60 * 60 * 24 * 30);
        String lastMonthStrDate = getSimpleDateFormatString(lastMonth, DATE_FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
        System.out.println("过去一个月的时间：" + lastMonthStrDate);

        // 过去一年
        Date lastYear = new Date(currentTime - 1000L * 60 * 60 * 24 * 366);
        String lastYearStrDate = getSimpleDateFormatString(lastYear, DATE_FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
        System.out.println("过去一年的时间：" + lastYearStrDate);
    }

    public String getSimpleDateFormatString(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    @Test
    public void testCurrentTime() throws ParseException {
        String lastTime = "2024-10-30T17:00".replace("T", " ");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = new Date();
        long currentTime = date.getTime() - 1000L * 60 * 60 * 6;

        Date last6Hour = new Date(currentTime);
        boolean after = last6Hour.after(sdf.parse(lastTime));
        System.out.println(after);
    }

//    public void cleanRedisData(int maxTime) throws ParseException {
//        QueryWrapper<WmsSend> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("update_time").last("limit 1");
//        WmsSend wmsSend = iWmsSendService.getOne(queryWrapper);
//        String lastTime = wmsSend.getUpdateTime().toString().replace("T", " ");
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//        Date date = new Date();
//        long currentTime = date.getTime() - 1000L * 60 * 60 * maxTime;
//
//        Date last6Hour = new Date(currentTime);
//        if (last6Hour.after(sdf.parse(lastTime))) {
//            log.info("开始清除redis数据，最后一条数据：" + wmsSend + "，时间跨度是：" + maxTime);
//        }
//    }

}